package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlarmRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则管理")
public class AlarmRuleController {

    @Autowired
    private AlarmRuleService alarmRuleService;

    @PostMapping("/create")
    @ApiOperation(value = "添加报警规则")
    public ResponseResult create(@RequestBody AlertRuleDto alertRuleDto) {
        alarmRuleService.create(alertRuleDto);
        return ResponseResult.success();
    }

    @GetMapping("/get-page")
    @ApiOperation( "分页查询报警规则")
    public ResponseResult<PageResponse<AlertRuleVo>> page(@RequestParam(value = "pageNum", required = true) Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                                          @RequestParam(value = "alertRuleName",required = false) String alertRuleName,
                                                          @RequestParam(value = "functionName",required = false) String functionName,
                                                          @RequestParam(value = "productKey",required = false) String productKey) {
        PageResponse<AlertRuleVo> alarmRuleVo= alarmRuleService.page(pageNum, pageSize, alertRuleName, functionName, productKey);

        return ResponseResult.success(alarmRuleVo);
    }

    @GetMapping("/read/{id}")
    @ApiOperation(value = "根据ID查询报警规则")
    public ResponseResult<AlertRuleVo> read(@PathVariable("id") Integer id) {
        AlertRuleVo alarmRuleVo = alarmRuleService.read(id);
        return ResponseResult.success(alarmRuleVo);
    }

    @PutMapping("/update/{id}")
    @ApiOperation(value = "根据ID更新报警规则")
    public ResponseResult update(@PathVariable("id") Long id,@RequestBody AlertRuleDto alertRuleDto) {
        alarmRuleService.update(id,alertRuleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除报警规则")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        alarmRuleService.delete(id);
        return ResponseResult.success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用-禁用")
    public ResponseResult updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        alarmRuleService.updateStatus(id, status);
        return ResponseResult.success();
    }

}
